package com.legolas.springcloud.springcloudconsumer.controller;

import com.legolas.springcloud.springcloudconsumer.client.TestClient;
import com.netflix.discovery.converters.Auto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author legolas
 * @date 2019/6/4 20:27
 */
@RestController
public class TestController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DiscoveryClient client;
    @Autowired
    TestClient testClient;
    @Autowired
    LoadBalancerClient loadBalancerClient;

    @RequestMapping("test1/get/{name}")
    public String getTest1(@PathVariable String name) {
        //return this.restTemplate.getForObject("http://springcloud-provider/test1/{1}", String.class, name);
        return testClient.test1(name);
    }

    @RequestMapping("/discover")
    public Object discover() {  // 直接返回发现服务信息
        return this.client;
    }

    @GetMapping("/log-user-service-instance")
    public void logUserServiceInstance() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("springcloud-provider");
        logger.info("serviceInstance info ---> serviceId is  " + serviceInstance.getServiceId() + " host is " + serviceInstance.getHost() + "port is " + serviceInstance.getPort());
    }

}
